import { useUserStore } from '@/store'
const tokenURL = 'https://sig-liveroom-admin.zego.cloud/thirdToken/get'

const userStore = useUserStore()

export const getLoginToken = (): Promise<string> => {
  return new Promise((resolve, reject) => {
    if (userStore.token) {
      resolve(userStore.token)
    }
    // #ifdef H5
    uni.request({
      url: tokenURL, // 该接口由开发者后台自行实现，开发者的 Token 从各自后台获取
      method: 'post',
      data: {
        appId: userStore.appId,
        idName: userStore.userId,
        version: '03',
        roomId: userStore.roomId,
        privilege: {
          '1': 1,
          '2': 1,
        },
        expire_time: 7 * 24 * 60 * 60,
      },
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.statusCode === 200) resolve(res.data?.data?.token as string)
        else reject(res)
      },
      fail(err) {
        reject(err)
      },
    })
    // #endif
  })
}
